package com.example.xunua_zhinengjiaotong;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.xunua_zhinengjiaotong.adapter.MyViewpagerAdapter;
import com.example.xunua_zhinengjiaotong.fragment.aboutFragment;
import com.example.xunua_zhinengjiaotong.fragment.homeFragment;
import com.example.xunua_zhinengjiaotong.fragment.setFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private FragmentManager fragmentManager;
    private TextView tvTitle,tvHome,tvSet,tvAbout;
    private ViewPager viewPager;
    private List<Fragment> fragmentList=new ArrayList<>();
    private MyViewpagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager=getSupportFragmentManager();
        initView();
        initFragment();
        adapter=new MyViewpagerAdapter(fragmentManager,MainActivity.this,fragmentList);
        viewPager.setAdapter(adapter);
        setMenuButton();
    }

    private void setMenuButton() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTv(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 设置菜单栏按钮颜色全部为未点击的状态
     * 并给点击的菜单设置颜色
     */
    private void setTv(int num){
        tvHome.setTextColor(getResources().getColor(R.color.main1));
        Drawable drawable1=getResources().getDrawable(R.mipmap.icon_home_nor);
        drawable1.setBounds(0,0,drawable1.getMinimumWidth(),drawable1.getMinimumHeight());
        tvHome.setCompoundDrawables(null,drawable1,null,null);

        tvSet.setTextColor(getResources().getColor(R.color.main1));
        Drawable drawable2=getResources().getDrawable(R.mipmap.icon_meassage_nor);
        drawable2.setBounds(0,0,drawable2.getMinimumWidth(),drawable2.getMinimumHeight());
        tvSet.setCompoundDrawables(null,drawable2,null,null);

        tvAbout.setTextColor(getResources().getColor(R.color.main1));
        Drawable drawable3=getResources().getDrawable(R.mipmap.icon_more_nor);
        drawable3.setBounds(0,0,drawable3.getMinimumWidth(),drawable3.getMinimumHeight());
        tvAbout.setCompoundDrawables(null,drawable3,null,null);
        switch (num){
            case 0:
                tvHome.setTextColor(getResources().getColor(R.color.main0));
                Drawable click1=getResources().getDrawable(R.mipmap.icon_home_sel);
                click1.setBounds(0,0,click1.getMinimumWidth(),click1.getMinimumHeight());
                tvHome.setCompoundDrawables(null,click1,null,null);
                tvTitle.setText("智能交通");
                break;
            case 1:
                tvSet.setTextColor(getResources().getColor(R.color.main0));
                Drawable click2=getResources().getDrawable(R.mipmap.icon_meassage_sel);
                click2.setBounds(0,0,click2.getMinimumWidth(),click2.getMinimumHeight());
                tvSet.setCompoundDrawables(null,click2,null,null);
                tvTitle.setText("设置");
                break;
            case 2:
                tvAbout.setTextColor(getResources().getColor(R.color.main0));
                Drawable click3=getResources().getDrawable(R.mipmap.icon_more_sel);
                click3.setBounds(0,0,click3.getMinimumWidth(),click3.getMinimumHeight());
                tvAbout.setCompoundDrawables(null,click3,null,null);
                tvTitle.setText("关于");
                break;
        }
    }

    /**
     * 初始化fragment
     */
    private void initFragment() {
        fragmentList.add(new homeFragment());
        fragmentList.add(new setFragment());
        fragmentList.add(new aboutFragment());
    }

    /**
     * 初始化控件
     */
    private void initView() {
        tvTitle=findViewById(R.id.main_tv_title);
        tvHome=findViewById(R.id.main_tv_home);
        tvSet=findViewById(R.id.main_tv_set);
        tvAbout=findViewById(R.id.main_tv_about);
        viewPager=findViewById(R.id.main_vp);

        tvHome.setOnClickListener(this);
        tvSet.setOnClickListener(this);
        tvAbout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_tv_home:
                setTv(0);
                viewPager.setCurrentItem(0);
                break;
            case R.id.main_tv_set:
                setTv(1);
                viewPager.setCurrentItem(1);
                break;
            case R.id.main_tv_about:
                setTv(2);
                viewPager.setCurrentItem(2);
                break;
        }
    }
    //屏蔽返回键
    @Override
    public void onBackPressed() {
        // super.onBackPressed();
    }
}
